package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.*;
import net.qhzw.vip.qiyu.mapper.ContractItemMapper;
import net.qhzw.vip.qiyu.model.CarRentalCostsItem;
import net.qhzw.vip.qiyu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.qhzw.vip.qiyu.util.SecurityUtils;

import java.math.BigDecimal;

@Service
public class ContractItemServiceImpl extends ServiceImpl<ContractItemMapper, ContractItem> implements ContractItemService {

    @Autowired
    private GuaranteeCompanyService guaranteeCompanyService;


    @Autowired
    private SysTenantIdentityService sysTenantIdentityService;


    @Autowired
    private RepairCodeService repairCodeService;

    @Autowired
    private DtsUserService dtsUserService;

    @Autowired
    private ContractService contractService;


    /***
     * 提交合同
     * @param contractItem
     * @return
     */
    @Override
    public boolean submit(ContractItem contractItem) {
        Long userId = SecurityUtils.getUserId();
        check(contractItem);

        long orderId = IdWorker.getId();
        contractItem.setId(orderId);


        Long contractItemId = null;
        contractService.addOrderId(orderId, contractItemId);
        return false;
    }


    /***
     * 结算费用明细
     * @param contractItem
     */
    @Override
    public CarRentalCostsItem chargeDetails(ContractItem contractItem) {
        check(contractItem);
        CarRentalCostsItem carRentalCostsItem = new CarRentalCostsItem();
        //查询用户
        DtsUser dtsUser = dtsUserService.getOne(new LambdaQueryWrapper<DtsUser>()
                .eq(DtsUser::getStatus, 1)
                .eq(contractItem.getUserId() != null, DtsUser::getId, contractItem.getUserId()));
        if (dtsUser != null) {
            carRentalCostsItem.setUsername(dtsUser.getRealName());
            carRentalCostsItem.setIdCard(dtsUser.getIdCard());
            carRentalCostsItem.setMobile(dtsUser.getMobile());
        }
        //BigDecimal totalMoney = new BigDecimal(0);
        carRentalCostsItem.setLease(contractItem.getLease());
        carRentalCostsItem.setVehicleRemark(contractItem.getVehicleRemark());
        //carRentalCostsItem.setTotalMoney(totalMoney);
        if (contractItem.getLease().intValue() == 1) { //1:整车 2:电池
            //查询车辆信息
            RepairCode repairCode = repairCodeService.getById(contractItem.getRepairCodeId());
            /*if (repairCode != null) {
                carRentalCostsItem.setVehicleBelongs(repairCode.getProdType());
                carRentalCostsItem.setCarNumber(repairCode.getProdNum());
                carRentalCostsItem.setVehicleDeposit(repairCode.getDeposit());
            }*/
        } else {
            carRentalCostsItem.setVehicleDeposit(new BigDecimal(500));
        }

        //查询担保公司
        GuaranteeCompany guaranteeCompany = guaranteeCompanyService.getById(contractItem.getGuarCompany());
        if (guaranteeCompany != null) {
            carRentalCostsItem.setGuarCompanyName(guaranteeCompany.getName());
            carRentalCostsItem.setGuarCompanyDeposit(guaranteeCompany.getDeposit());
            return carRentalCostsItem;
        }
        //租车身份
        SysTenantIdentity tenantIdentity = sysTenantIdentityService.getById(contractItem.getCarRentalStatus());
        if (tenantIdentity != null) {
            carRentalCostsItem.setTenantIdentityName(tenantIdentity.getIdentityName());
            carRentalCostsItem.setMinFrstCrg(tenantIdentity.getMinFrstCrg());
        }
        return carRentalCostsItem;
    }


    /***
     * 校验参数
     * @param contractItem
     */
    private void check(ContractItem contractItem) {

    }
}
